package web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import objects.Board;
import objects.ConfigurationType;
import objects.Player;
import utils.FactoryUI;

import com.google.gson.Gson;

/**
 * Servlet implementation class PlayerServlet
 */
@WebServlet(name = "Player", urlPatterns =
{
    "/Player"
})
public class PlayerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PlayerServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setContentType("application/json");
	    response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.flush();
		resultJson rj = new resultJson();
		
		Player player = Board.getInstance().getCurrPlayerInstance();
		if (player != null)
		{
			String newPlayer = request.getParameter("newPlayer");
			String room = Board.getInstance().getRoomName(Integer.valueOf(request.getParameter("roomNumber")));
			
			if (newPlayer != null && !newPlayer.equals(""))
			{
				rj.html = "<label>Current Player: " + newPlayer + " Current room: "+ room + "</label> \r\n";
			}
			else
			{
				rj.html = "<label>Current Player: " + player.getPlayerName() + " Current room: "+ room + "</label> \r\n";
			}
			
			
			List<String> playerDetails = new ArrayList<String>();
			
			if (Board.getInstance().getBdGameData().getType().equals(ConfigurationType.MANUAL))
			{
			
				for (int i=0; i < Board.getInstance().getNumberOfPlayersJoined(); i++) {
					
					Player currPlayer = Board.getInstance().getBdGameData().getPlayers().get(i);
					
					playerDetails.add(currPlayer.getPlayerName() + " (" + currPlayer.getType().toString() + ") " + currPlayer.getCharacter().name() + " " + currPlayer.getReason());
				}
			}
			else
			{
				for (Player currPlayer : Board.getInstance().getBdGameData().getPlayers()) {
					if (currPlayer.isOccupied() || currPlayer.getType().equals("COMPUTER"))
					{
						playerDetails.add(currPlayer.getPlayerName() + " (" + currPlayer.getType().toString() + ") " + currPlayer.getCharacter().name() + " " + currPlayer.getReason());
					}
				}
			}
			
			rj.playerList = FactoryUI.getListHtml(playerDetails);
			rj.roomNumber = player.getRoomNumber();
			rj.playerType = player.getType();
			rj.currPlayer = player.getPlayerName();
			out.print(new Gson().toJson(rj));
		}
		
	}
	
	@SuppressWarnings("unused")
	private class resultJson
	{
		String html;
		String playerList;
		String playerType;
		String currPlayer;
		int roomNumber;
	}

}
